package br.com.covosys.model.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import br.com.covosys.dao.GenericDAO;
import br.com.covosys.model.GenericModel;
import br.com.covosys.model.ModelException;

public abstract class GenericModelImpl<BEAN extends Serializable, DAO extends GenericDAO<BEAN>> implements GenericModel<BEAN, DAO> {

	private static final long serialVersionUID = 1L;

	protected DAO dao;
	
	public GenericModelImpl(DAO dao) {
		this.dao = dao;
	}
	
	@Override
	public void save(BEAN bean) throws ModelException {
		try {
			if (validate()) {
				dao.save(bean);
			}
		} catch (Exception exception) {
			throw new ModelException(exception);
		}
	}

	@Override
	public void delete(BEAN bean) throws ModelException {
		try {
			dao.delete(bean);
		} catch (Exception exception) {
			throw new ModelException(exception);
		}
	}

	@Override
	public BEAN retrieve(BEAN bean) throws ModelException {
		try {
			return dao.retrieve(bean);
		} catch (Exception exception) {
			throw new ModelException(exception);
		}
	}

	@Override
	public List<BEAN> list() throws ModelException {
		try {
			return dao.list();
		} catch (Exception exception) {
			throw new ModelException(exception);
		}
	}
	
	public List<BEAN> list(Map<String, Object> parametros) throws ModelException {
		try {
			return dao.list(parametros);
		} catch (Exception exception) {
			throw new ModelException(exception);
		}
	}

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}
	
	protected boolean validate() throws ModelException {
		return true;
	}
}
